﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Core;
using Newtonsoft.Json;

namespace WinClock
{
    public partial class WinForm : Form
    {
        /// <summary>
        /// 软件名称
        /// </summary>
        public string softName = "WindowsClock";
        /// <summary>
        /// 获取当前路径
        /// </summary>
        public string projectPath = System.Environment.CurrentDirectory;
        /// <summary>
        /// 获取服务器时间URL
        /// </summary>
        public string urlTime = "https://f.m.suning.com/api/ct.do";
        public WinForm()
        {
            InitializeComponent();
            // 窗口居中
            this.StartPosition = FormStartPosition.CenterScreen;
            // windows 分辨率大小
            int winWidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
            int winHeight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            // 设置WinForms窗口大小
            Size size = new Size(380, 235);
            this.Size = size;
            this.Text = softName;
            this.timerSend.Tick += new System.EventHandler(this.timerSend_Tick);
        }
        private void loadTime()
        {
            try
            {
                string j = HttpHelper.Get(this.urlTime);
                Object obj = JsonConvert.DeserializeObject(j);
                Newtonsoft.Json.Linq.JObject js = obj as Newtonsoft.Json.Linq.JObject;//把上面的obj转换为 Jobject对象
                string time = (Convert.ToInt64(js["currentTime"].ToString()) / 1000).ToString();
                DateTime dt = DateTimeHelper.GetTimeFromUint(time);
                this.lblTime.Text = dt.ToString("HH:mm:ss");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
                throw;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public Font ShowFont(float size)
        {
            Font font = null;
            System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
            string fontsPath = projectPath + @"\fonts\YournameD7HomeHalf.ttf";
            privateFonts.AddFontFile(fontsPath);
            font = new Font(privateFonts.Families[0], size);
            return font;
        }

        private void timerSend_Tick(object sender, EventArgs e)
        {
            this.lblTime.Font = ShowFont(60);
            loadTime();
        }

        private void cbShow_CheckedChanged(object sender, EventArgs e)
        {
            if (((CheckBox)sender).CheckState.ToString() == "Checked")
            {
                // 打开另一个窗口的同时隐藏当前窗口
                Clock formMain = Clock.GetInstance();
                if (formMain != null)
                {
                    formMain.Show();
                }
            }
            else
            {
                Clock formMain = Clock.GetInstance();
                if (formMain != null)
                {
                    formMain.Close();
                }
            }
        }
    }
}
